﻿/* 
* Copyright (c) 2009, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Bio.Data;
using Microsoft.Windows.Controls;

namespace Bio.Views.Alignment.Views
{
    class DataGridNucleotideColumn : DataGridBoundColumn
    {
        public int CellIndex { get; set; }
        
        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            var nc = new NucleotideVisual();

            nc.SetBinding(FrameworkElement.DataContextProperty, Binding);
            nc.SetBinding(NucleotideVisual.NucleotideProperty, new Binding("Value"));

            return nc;
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            return GenerateElement(cell, dataItem);
        }
    }

    public class NucleotideVisual : FrameworkElement
    {
        static readonly Rect RcBounds = new Rect(4, -2, 18, 16);
        static readonly Rect RcBorder = new Rect(-1, -1, 20, 20);

        #region EffectBrush

        /// <summary>
        /// EffectBrush Dependency Property
        /// </summary>
        public static readonly DependencyProperty EffectBrushProperty =
            DependencyProperty.Register("EffectBrush", typeof(Brush), typeof(NucleotideVisual),
                                        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));

        /// <summary>
        /// Gets or sets the EffectBrush property.  This dependency property 
        /// indicates ....
        /// </summary>
        public Brush EffectBrush
        {
            get { return (Brush)GetValue(EffectBrushProperty); }
            set { SetValue(EffectBrushProperty, value); }
        }

        #endregion

        #region Nucleotide
        /// <summary>
        /// Nucleotide character property
        /// </summary>
        public static readonly DependencyProperty NucleotideProperty =
            DependencyProperty.Register("Nucleotide", typeof(BioSymbol), typeof(NucleotideVisual),
                                        new FrameworkPropertyMetadata(BioSymbol.None, FrameworkPropertyMetadataOptions.AffectsRender));
        /// <summary>
        /// Gets or sets the Nucleotide property.
        /// </summary>
        public BioSymbol Nucleotide
        {
            get { return (BioSymbol)GetValue(NucleotideProperty); }
            set { SetValue(NucleotideProperty, value); }
        }
        #endregion

        protected override void OnRender(DrawingContext dc)
        {
            if (EffectBrush != null)
                dc.DrawRectangle(EffectBrush, null, RcBorder);

            var symbol = DetermineVisual();
            if (symbol != null)
                dc.DrawImage(symbol, RcBounds);
        }

        private ImageSource DetermineVisual()
        {
            BioSymbol newValue = Nucleotide;
            if (newValue == null)
                return null;

            ImageSource symbol;

            switch (newValue.Type)
            {
                case BioSymbolType.Nucleotide:
                    switch (Char.ToUpper(newValue.Value))
                    {
                        case 'A':
                            symbol = Nucleotide_A;
                            break;
                        case 'G':
                            symbol = Nucleotide_G;
                            break;
                        case 'C':
                            symbol = Nucleotide_C;
                            break;
                        case 'I':
                            symbol = Nucleotide_I;
                            break;
                        case 'U':
                            symbol = Nucleotide_U;
                            break;
                        default:
                            symbol = Unknown_Nucleotide;
                            break;
                    }
                    break;
                case BioSymbolType.Missing:
                    symbol = Nucleotide_Missing;
                    break;
                default:
                    symbol = Nucleotide_None;
                    break;
            }

            return symbol;
        }

        private static ImageSource _nucleotideA;
        private static ImageSource _nucleotideG;
        private static ImageSource _nucleotideC;
        private static ImageSource _nucleotideI;
        private static ImageSource _nucleotideU;
        private static ImageSource _nucleotideMissing;
        private static ImageSource _nucleotideNone;
        private static ImageSource _unknown;

        ImageSource Nucleotide_A
        {
            get
            {
                if (_nucleotideA == null)
                    _nucleotideA = GenerateImage((PathGeometry)TryFindResource("Nucleotide_A"), Brushes.Blue, Brushes.DarkBlue);
                return _nucleotideA;
            }
        }

        ImageSource Nucleotide_G
        {
            get
            {
                if (_nucleotideG == null)
                    _nucleotideG = GenerateImage((PathGeometry)TryFindResource("Nucleotide_G"), Brushes.Green, Brushes.DarkGreen);
                return _nucleotideG;
            }
        }

        ImageSource Nucleotide_C
        {
            get
            {
                if (_nucleotideC == null)
                    _nucleotideC = GenerateImage((PathGeometry)TryFindResource("Nucleotide_C"), Brushes.Red, Brushes.DarkRed);
                return _nucleotideC;
            }
        }

        ImageSource Nucleotide_I
        {
            get
            {
                if (_nucleotideI == null)
                    _nucleotideI = GenerateImage((PathGeometry)TryFindResource("Nucleotide_I"), Brushes.Indigo, Brushes.Purple);
                return _nucleotideI;
            }
        }

        ImageSource Nucleotide_U
        {
            get
            {
                if (_nucleotideU == null)
                    _nucleotideU = GenerateImage((PathGeometry)TryFindResource("Nucleotide_U"), Brushes.Violet, Brushes.Black);
                return _nucleotideU;
            }
        }

        ImageSource Nucleotide_Missing
        {
            get
            {
                if (_nucleotideMissing == null)
                    _nucleotideMissing = GenerateImage((PathGeometry)TryFindResource("Nucleotide_Missing"), Brushes.LightGray, null);
                return _nucleotideMissing;
            }
        }

        ImageSource Nucleotide_None
        {
            get
            {
                if (_nucleotideNone == null)
                    _nucleotideNone = GenerateImage((PathGeometry)TryFindResource("Nucleotide_None"), Brushes.Black, null);
                return _nucleotideNone;
            }
        }

        ImageSource Unknown_Nucleotide
        {
            get
            {
                if (_unknown == null)
                    _unknown = GenerateImage((PathGeometry)TryFindResource("Nucleotide_Unknown"), Brushes.Pink, Brushes.Red);
                return _nucleotideNone;
            }
        }

        /// <summary>
        /// This generates an Image from a given geometry and caches it off.
        /// </summary>
        /// <param name="geometry"></param>
        /// <param name="brush"></param>
        /// <param name="penColor"></param>
        /// <returns></returns>
        private static BitmapSource GenerateImage(Geometry geometry, Brush brush, Brush penColor)
        {
            if (geometry == null)
                return null;

            DrawingVisual drawingVisual = new DrawingVisual();
            DrawingContext drawingContext = drawingVisual.RenderOpen();
            drawingContext.DrawGeometry(brush, new Pen(penColor, 1), geometry);
            drawingContext.Close();

            RenderTargetBitmap tb = new RenderTargetBitmap(15, 15, 96, 96, PixelFormats.Default);
            tb.Render(drawingVisual);
            tb.Freeze();

            return tb;
        }
    }
}